<?php
if(!defined('FROM_INDEX'))exit('Invalid request');

require_once ROOT_PATH.'include/sql/class_db.php';
require_once ROOT_PATH.'include/sql/base.sql.php';

class UserSqlFunc extends BaseSqlFunc
{	
	/**
	 *
	 * 根据ID获取用户信息
	 *
	 * @param String $id
	 */
	function get($id)
	{
		$sql = <<<SQL
select
	u.*, ui.*
from
	`{$this->pre}user` u
	inner join `{$this->pre}user_info` ui on u.id=ui.user_id
where
	u.`email` = ?
	or u.`id` = ?
	or u.`name` = ?
	or u.source_id = ?
SQL;

		$result = DBFunction::getInstance()->getRow($sql, array($id, $id, $id, $id));

		return check_array($result) ? $result : array();
	}

	/**
	 *
	 * 搜索用户
	 *
	 * @param String $id
	 */
	function search($request) {
		$name = '%'.$request['name'].'%';
		$limit = $request['page_size'];
		$num = $request['page_index'];

		$sql = <<<SQL
select
	count(*)
from
	`{$this->pre}user` u
where
	u.`name` like ?
SQL;
		
		$count = DBFunction::getInstance()->getOne($sql, array($name));
		if ($count < 1) return array();
		
		$sql = <<<SQL
select
	u.id, u.`name`, u.`image`, u.`gender`, u.city
from
	`{$this->pre}user` u
where
	u.`name` like ?
SQL;

		$list = DBFunction::getInstance()->selectLimit($sql,$limit, $num, array($name));

		$result['total_count'] = $count;
		$result['list'] = $list;
		
		return $result;
	}

	/**
	 *
	 * 新增用户
	 * @param array $request
	 */
	function add($request)
	{
		$params = array(
		$request['source_id'],
		$request['name'],
		$request['email'],
		$request['desc'],
		$request['password'],
		$request['image'],
		$request['gender'],
		$request['city'],
		$request['time'],
		$request['salt']
		);

		$sql = <<<SQL
insert into `{$this->pre}user`
	(`source_id`, `name`, `email`, `desc`, 
	`password`, `image`,
	`gender`, `city`, `register_time`, `salt`)
values
	(?,?,?,?,
	?,?,
	?,?,?,?)
SQL;

		$result = DBFunction::getInstance()->insert($sql, $params);

		if ($result !== false) {
			$params = array(
			$result,
			0,
			0,
			0,
			0,
			$request['time']
			);

			$sql = <<<SQL
insert into `{$this->pre}user_info`
	(`user_id`, `fans_count`, `follow_count`, `story_count`,
	`tag_count`, `last_login`)
values
	(?,?,?,?,
	?,?)
SQL;
			DBFunction::getInstance()->insert($sql, $params);
		}

		return ($result !== false) ? $result : false;
	}

	/**
	 *
	 * 更新用户
	 * @param array $request
	 */
	function update($request)
	{
		$params = array(
		$request['name'],
		$request['desc'],
		$request['password'],
		$request['image'],
		$request['gender'],
		$request['city'],
		$request['id']
		);

		$sql = <<<SQL
update `{$this->pre}user`
set 
	`name`=?,
	`desc`=?,
	`password`=?, 
	`image`=?,
	`gender`=?, 
	`city`=?
where
	id=?
SQL;

		$result = DBFunction::getInstance()->update($sql, $params);
		
		return ($result !== false) ? true : false;
	}

	/**
	 * 
	 * Enter description here ...
	 * @param array $request
	 */
	function update_last_login($request)
	{
		$params = array(
		$request['time'],
		$request['id']
		);

		$sql = <<<SQL
update `{$this->pre}user_info`
set 
	`last_login`=?
where
	id=?
SQL;

		$result = DBFunction::getInstance()->update($sql, $params);

		return ($result !== false) ? true : false;
	}
	
	/**
	 * 
	 * Enter description here ...
	 * @param int $uid
	 */
	function update_story_count($uid)
	{
		$sql = <<<SQL
update `{$this->pre}user_info`
set 
	`story_count`=(select count(*) from `{$this->pre}story` where `user_id`=?)
where
	`user_id`=?
SQL;

		$result = DBFunction::getInstance()->update($sql, array($uid,$uid));
		return ($result !== false) ? true : false;
	}
	
	/**
	 * 
	 * Enter description here ...
	 * @param int $uid
	 */
	function update_fans_count($uid)
	{
		$sql = <<<SQL
update `{$this->pre}user_info`
set 
	`fans_count`=(select count(*) from `{$this->pre}user_follow` where `f_user_id`=?)
where
	`user_id`=?
SQL;

		$result = DBFunction::getInstance()->update($sql, array($uid, $uid));
		return ($result !== false) ? true : false;
	}
	
	/**
	 * 
	 * Enter description here ...
	 * @param unknown_type $uid
	 */
	function add_follow($request)
	{
		$sql = <<<SQL
select
	count(*)
from 
	`{$this->pre}user_follow`
where 
	`user_id`=?
	and `f_user_id`=?
SQL;

		$result = DBFunction::getInstance()->getOne($sql, array($request['uid'], $request['id']));
		if ($result > 0) return false;

		$params = array(
			$request['uid'],
			$request['id'],
			time()
			);

		$sql = <<<SQL
insert into `{$this->pre}user_follow`
	(`user_id`, `f_user_id`, `create_time`)
values
	(?,?,?)
SQL;

		$result = DBFunction::getInstance()->insert($sql, $params);

		return ($result !== false) ? true : false;
	}
	
	/**
	 * 
	 * Enter description here ...
	 * @param unknown_type $uid
	 */
	function remove_follow($request)
	{
		$sql = <<<SQL
select
	count(*)
from 
	`{$this->pre}user_follow`
where 
	`user_id`=?
	and `f_user_id`=?
SQL;

		$result = DBFunction::getInstance()->getOne($sql, array($request['uid'], $request['id']));
		if ($result < 1) return false;
		
		$params = array(
			$request['uid'],
			$request['id'],
			time()
			);

		$sql = <<<SQL
delete from `{$this->pre}user_follow`
where 
	`user_id`=?
	and `f_user_id`=?
SQL;

		$result = DBFunction::getInstance()->execute($sql, $params);

		return ($result !== false) ? true : false;
	}
	
	/**
	 *
	 * Enter description here ...
	 * @param unknown_type $uid
	 */
	function check_follow($request)
	{
		$params = array(
		$request['uid'],
		$request['id']
		);

		$sql = <<<SQL
select 
	create_time 
from `{$this->pre}user_follow`
where 
	`user_id`=?
	and `f_user_id`=?
SQL;

		$result = DBFunction::getInstance()->getOne($sql, $params);
		//var_dump($params, $result);

		return ($result != '') ? 1 : 0;
	}
	
	/**
	 * 
	 * Enter description here ...
	 * @param unknown_type $uid
	 */
	function update_follow_count($request)
	{
		$sql = <<<SQL
update `{$this->pre}user_info`
set 
	`follow_count`=(select count(*) from `{$this->pre}user_follow` where `user_id`=?)
where
	`user_id`=?
SQL;

		$result = DBFunction::getInstance()->update($sql, array($request['uid'], $request['uid']));
		
		$sql = <<<SQL
update `{$this->pre}user_info`
set 
	`fans_count`=(select count(*) from `{$this->pre}user_follow` where `f_user_id`=?)
where
	`user_id`=?
SQL;

		$result = DBFunction::getInstance()->update($sql, array($request['id'], $request['id']));
		
		return ($result !== false) ? true : false;
	}
	
	function update_tag_count($uid)
	{
		$sql = <<<SQL
update `{$this->pre}user_info`
set 
	`tag_count`=(select count(*) from `{$this->pre}tag` where `user_id`=?)
where
	`user_id`=?
SQL;

		$result = DBFunction::getInstance()->update($sql, array($uid, $uid));
		return ($result !== false) ? true : false;
	}
	
	/**
	 * TODO
	 *
	 * 删除用户
	 * @param String $uids
	 */
	function delete($uids)
	{
		return false;
	}

	/**
	 *
	 * 获取用户动态
	 * @param array $request
	 */
	function get_feed($request)
	{
		$name = $request['user_id'];
		$limit = $request['page_size'];
		$num = $request['page_index'];

		$sql = <<<SQL
select
	f.*, 
	u.`name`, u.`image`, u.`gender`, 
	s.*
from
	`{$this->pre}user_feed` f
left out join `{$this->pre}user` u on f.`f_user_id` = u.id
left out join `{$this->pre}story` s on f.`f_story_id` = s.id
where
	f.user_id = ?
SQL;

		$result = DBFunction::getInstance()->selectLimit($sql,$limit, $num, array($name));

		return check_array($result) ? $result : array();
	}

	/**
	 *
	 * 获取用户关注列表
	 * @param array $request
	 */
	function get_follow_list($request)
	{
		$uid = $request['uid'];
		$id = $request['id'];
		$limit = $request['page_size'];
		$num = $request['page_index'];

		$sql = <<<SQL
select
	count(*)
from
	`{$this->pre}user_follow` f
	inner join `{$this->pre}user` u on u.id = f.f_user_id
	inner join `{$this->pre}user_info` ui on ui.user_id = f.user_id
	left outer join `{$this->pre}user_follow` uf on uf.user_id=? and uf.f_user_id=u.id
where
	f.user_id = ?
SQL;
		
		$count = DBFunction::getInstance()->getOne($sql, array($uid, $id));
		if ($count < 1) return array();
		
		$sql = <<<SQL
select
	u.`id`, u.`name`, u.`image`, u.`gender`,ui.fans_count,ui.follow_count,
	uf.user_id <> '' as "is_follow"
from
	`{$this->pre}user_follow` f
	inner join `{$this->pre}user` u on u.id = f.f_user_id
	inner join `{$this->pre}user_info` ui on ui.user_id = f.user_id
	left outer join `{$this->pre}user_follow` uf on uf.user_id=? and uf.f_user_id=u.id
where
	f.user_id = ?
order by f.create_time desc
SQL;

		$list = DBFunction::getInstance()->selectLimit($sql,$limit, $num, array($uid, $id));

		$result['total_count'] = $count;
		$result['list'] = $list;
		
		return $result;
	}

	/**
	 *
	 * 获取用户粉丝列表
	 * @param array $request
	 */
	function get_fans_list($request)
	{
		$uid = $request['uid'];
		$id = $request['id'];
		$limit = $request['page_size'];
		$num = $request['page_index'];

		$sql = <<<SQL
select
	count(*)
from
	`{$this->pre}user_follow` f
	inner join `{$this->pre}user` u on u.id = f.user_id
	inner join `{$this->pre}user_info` ui on ui.user_id = f.user_id
	left outer join `{$this->pre}user_follow` uf on uf.user_id=? and uf.f_user_id=u.id
where
	f.f_user_id = ?
SQL;
		
		$count = DBFunction::getInstance()->getOne($sql, array($uid, $id));
		if ($count < 1) return array();
		
		$sql = <<<SQL
select
	u.`id`, u.`name`, u.`image`, u.`gender`,ui.fans_count,ui.follow_count,
	uf.user_id <> '' as "is_follow"
from
	`{$this->pre}user_follow` f
	inner join `{$this->pre}user` u on u.id = f.user_id
	inner join `{$this->pre}user_info` ui on ui.user_id = f.user_id
	left outer join `{$this->pre}user_follow` uf on uf.user_id=? and uf.f_user_id=u.id
where
	f.f_user_id = ?
order by f.create_time desc
SQL;

		$list = DBFunction::getInstance()->selectLimit($sql,$limit, $num, array($uid, $id));

		$result['total_count'] = $count;
		$result['list'] = $list;
		
		return $result;
	}
	
	/**
	 *
	 * 获取推荐用户列表
	 * @param array $request
	 */
	function get_user_recommend($request)
	{
		$uid = $request['uid'];
		$limit = $request['page_size'];
		$num = $request['page_index'];
		
		$sql = <<<SQL
select
	u.`id`, u.`name`, u.`image`, u.`gender`,ui.fans_count,ui.follow_count,
	uf.user_id <> '' as "is_follow"
from
	`{$this->pre}user` u
	inner join `{$this->pre}user_info` ui on ui.user_id = u.id
	left outer join `{$this->pre}user_follow` uf on uf.user_id=? and uf.f_user_id=u.id
order by ui.story_count desc, ui.fans_count desc
limit 30
SQL;

		//$list = DBFunction::getInstance()->selectLimit($sql,$limit, $num, array($uid));
		$list = DBFunction::getInstance()->getAll($sql, array($uid));

		//$result['total_count'] = 30;
		$result['list'] = $list;
		
		return $result;
	}
	
	function add_feedback($request)
	{
		$params = array(
		$request['uid'],
		$request['story_id'],
		$request['type'],
		$request['content'],
		time()
		);

		$sql = <<<SQL
insert into `{$this->pre}feedback`
	(`user_id`, `story_id`, `type`, `content`, `create_time`)
values
	(?,?,?,?,?)
SQL;

		$result = DBFunction::getInstance()->insert($sql, $params);
		
		return ($result !== false) ? $result : false;
	}
}